package cn.zhz.dao;

import cn.zhz.pojo.Dept;
import org.hibernate.Query;
import org.hibernate.Transaction;

import java.util.List;

public class DeptDao extends BaseDao{
    public void save(Dept dept){
        super.currentSession().save(dept);
    }

    public Dept get(int id){
        return (Dept) super.currentSession().get(Dept.class, id);
    }

    public Dept load(int id){
        return (Dept) super.currentSession().load(Dept.class, id);
    }

    public void delete(Dept dept){
        this.currentSession().delete(this.load(dept.getDeptNo()));
    }

    public void update(Dept dept){
        super.currentSession().update(dept);
    }

    public void saveOrUpdate(Dept dept){
        super.currentSession().saveOrUpdate(dept);
    }

    public Dept merge(Dept dept){
        return (Dept) super.currentSession().merge(dept);
    }


    public List<Dept> findDeptByName(String deptName){
        String hql = "from Dept as dept where dept.deptName = ?";
        Query query = currentSession().createQuery(hql);
        query.setString(0,deptName);
        return query.list();
    }

    public List<Dept> findByDeptName2(String deptName){
        //设置命名参数
        String hql = "from Dept as dept where dept.deptName = :name";
        Query query = currentSession().createQuery(hql);
        query.setString("name","SALES");
        return query.list();
    }

    public List<Dept> findAllDeptList_2() {
        String hql = "select new Dept(deptNo, deptName) from Dept";
        return currentSession().createQuery(hql).list();
    }

    public List<Object[]> findByInnerJoin(){
        String hql = "from Dept d inner join d.emps";
        return currentSession().createQuery(hql).list();
    }

    public List<Dept> findByInnerJoinFetch(){
        String hql = "from Dept d inner join fetch d.emps";
        return currentSession().createQuery(hql).list();
    }

    public List<Object[]> findByLeftJoin(){
        String hql = "from Dept d left join d.emps";
        return currentSession().createQuery(hql).list();
    }

    public List<Dept> findByleftJoinFetch(){
        String hql = "from Dept d left join fetch d.emps";
        return currentSession().createQuery(hql).list();
    }

    public List<Object[]> findByEqualJoin(){
        String hql = "from Dept d, Emp e where d = e.dept";
        return currentSession().createQuery(hql).list();
    }

    public Long findCount(){
        String hql = "select count(deptNo) from Dept d";
        return (Long) currentSession().createQuery(hql).uniqueResult();
    }

    public List<Dept> findBySal(){
        String hql = "from Dept d where 5000>  all(select e.sal from d.emps e)";
        return currentSession().createQuery(hql).list();
    }

    public List<String> findAllNames(){
        String hql = "select deptName from Dept";
        return currentSession().createQuery(hql).list();
    }

//    public List<Object[]> findAllDeptList(){
//        String hql = "select deptNo,deptName from Dept";
//        return currentSession().createQuery(hql).list();
//    }

    public List<Dept> findAllDeptList(){
        String hql = "select new Dept(deptNo,deptName) from Dept";
        return currentSession().createQuery(hql).list();
    }
}
